package org.reliablesource.prism.ui.popup.actions;

/**
 *
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Mar 11, 2006
 *	@author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

import java.util.Observable;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUnit;
import org.reliablesource.prism.core.IPrismUpdateEvent;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;

public class DeletePrismElementAction extends Action {

	protected IPrismElement selectedElement;

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		if(!isEnabled())
			return;

		IPrismTutorial tutorial = (IPrismTutorial) PrismCorePlugin.getDefault()
				.getModel().getChild(0);

		if (selectedElement == null || !(selectedElement instanceof IPrismUnit))
			return;

		if (tutorial.removeChild(selectedElement)) {
			IPrismUpdateEvent _updateEvent = PrismCorePlugin.getDefault().getElementFactory().createUpdateEvent(IPrismUpdateEvent.REMOVE);
			_updateEvent.addToChangedList(selectedElement);
			((Observable)PrismCorePlugin.getDefault().getModel()).notifyObservers(_updateEvent);
		}
	}

	public boolean setSelection(final ISelection _selection) {
		setEnabled(false);
		if (_selection != null) {
			Object _selectedObj = ((StructuredSelection) _selection)
					.getFirstElement();
			if (_selectedObj instanceof IPrismElement) {
				selectedElement = (IPrismElement) _selectedObj;
				setEnabled(true);
			}
		}
		return isEnabled();
	}
}
